package com.machine.action.admin;

import java.io.File;

import javax.annotation.Resource;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.machine.dao.user.UserDao;
import com.opensymphony.xwork2.ActionSupport;

@Component("recommendUpload")
@Scope("prototype")
public class RecommendUpload extends ActionSupport {

	private static final long serialVersionUID = 1L;

	private String mingcheng;
	private File image;
	private String imageFileName; // 文件名称
	private String imageContentType; // 文件类型
	private UserDao userDao;

	public UserDao getUserDao() {
		return userDao;
	}
	
	@Resource
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public String getImageFileName() {
		return imageFileName;
	}

	public void setImageFileName(String imageFileName) {
		this.imageFileName = imageFileName;
	}

	public String getImageContentType() {
		return imageContentType;
	}

	public void setImageContentType(String imageContentType) {
		this.imageContentType = imageContentType;
	}

	public String getMingcheng() {
		return mingcheng;
	}

	public void setMingcheng(String mingcheng) {
		this.mingcheng = mingcheng;
	}

	public File getImage() {
		return image;
	}

	public void setImage(File image) {
		this.image = image;
	}

	@Override
	public String execute() throws Exception {
		String realpath = ServletActionContext.getServletContext().getRealPath(
				"/upload");
		
		if(!userDao.uploadCheck()){
			ServletActionContext.getResponse().getWriter().write("out of bound!");
			return SUCCESS;
		}
		
		if (image != null) {
			File savefile = new File(new File(realpath), imageFileName);
			if (!savefile.getParentFile().exists())
				savefile.getParentFile().mkdirs();
			FileUtils.copyFile(image, savefile);
			// System.out.println("上传成功");
			String path = "/Machine/upload/"+imageFileName;
			userDao.insertUpload(path, mingcheng);
		}

		return SUCCESS;
	}
}
